<template>
    <div class="login-wrap">
        <!-- vue-particles https://www.cnblogs.com/tangwei89/p/11512804.html -->
        <vue-particles
        color="#fff"
        :particleOpacity="0.7"
        :particlesNumber="60"
        shapeType="circle"
        :particleSize="4"
        linesColor="#fff"
        :linesWidth="1"
        :lineLinked="true"
        :lineOpacity="0.4"
        :linesDistance="150"
        :moveSpeed="2"
        :hoverEffect="true"
        hoverMode="grab"
        :clickEffect="true"
        clickMode="push"
      >
        </vue-particles>
        <div class="ms-login">
            <div class="ms-title">注册用户</div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="param.username" placeholder="username">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input
                        type="password"
                        placeholder="password"
                        v-model="param.password"
                        @keyup.enter.native="RegisterForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="role">
                    <div class="prefix">
                        <el-button slot="prepend" icon="el-icon-lx-group"></el-button>
                    </div>
                    <el-select v-model="param.role" placeholder="role">
                        <el-option label="设计方" value="designer"></el-option>
                        <el-option label="施工方" value="builder"></el-option>
                        <el-option label="监理" value="watcher"></el-option>
                        <el-option label="业主" value="boss"></el-option>
                    </el-select>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="RegisterForm()">注册</el-button>
                </div>
                <a href="#" class="login-tips">忘记密码?</a>
                <a href="#" class="login-tips top-left"  @click.prevent="login">已有用户？</a>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data: function() {
        return {
            param: {
                username: '',
                password: '',
                role: 'boss',
            },
            rules: {
                username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
            },
        };
    },
    methods: {
        RegisterForm() {
            this.$refs.login.validate(valid => {
                if (valid) {
                    this.$axios({
                        method: 'post',
                        url: 'http://localhost:8080/user/register',
                        data: this.param,
                    })
                    // this.$axios.post('http://localhost:8080/user/register', this.param)
                    .then(res => {
                        this.$message({
                            message: '注册成功',
                            type: 'success',
                            center: true
                        });
                        this.$router.push('/login');
                    })
                    .catch(e => {
                        console.login(e);
                        this.$message({
                            message: '注册失败',
                            type: 'error',
                            center: true
                        });
                    })
                } else {
                    this.$message.error('账号或密码不符合规范！');
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        login(){
            this.$router.push('/login');
        }
    },
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    /* background-image: url(../../assets/img/login-bg.jpg); */
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);;;
}
.ms-login {
    z-index: 99;
    position: absolute;
    left: 50%;
    top: 50%;
    width: 450px;
    /* margin: -190px 0 0 -190px; */
    margin-left: -225px;
    margin-top: -160px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.05);
    overflow: hidden;
}
.prefix {
    display: inline-block;
    width: 45px;
    height: 31px;
    border-color: transparent;
    background-color: transparent;
    color: inherit;
    margin-right: -1px;
}
.el-select {
    width: 344px;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
.top-left {
    margin-left: 255px;
}
</style>